package bbs.mvc.Servlet;

import bbs.mvc.Filter.AuthFilter;
import bbs.mvc.Beans.DBUtils;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebServlet(name="signServlet",value = "/sign-Servlet")
public class SignServlet extends HttpServlet {
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
    this.doPost(request, response);
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
        request.setCharacterEncoding("UTF-8");
        String user = request.getParameter("username");
        String pass = request.getParameter("password");
        String code = request.getParameter("code");
        HttpSession session = request.getSession(true);
        String validateCode = (String)session.getAttribute(IdenterfyServlet.uri);

        if(!(validateCode.equals(code))){
            System.out.println("验证码内容错误 应当: "+session.getAttribute(IdenterfyServlet.uri));
            System.out.println("实际输入: "+session.getAttribute(code));
            response.sendRedirect("/signup.jsp/");
        }
        else{
            DBUtils.signup(user,pass);
            session.setAttribute(AuthFilter.LOGIN_STATUS, Boolean.TRUE);
            session.setAttribute("user",user);
            response.sendRedirect("./admin/Arstartes.jsp");
        }
    }
}
